﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShenLanCMS.Models;

namespace ShenLanCMS.Web.Controllers
{
    public class CommentController : Controller
    {
        private EntityContext db = new EntityContext();

        [IsAdmin]
        public ViewResult Index()
        {
            return View(db.Comments.ToList());
        }

        [IsAdmin]
        public ViewResult Details(Guid id)
        {
            Comment comment = db.Comments.Find(id);
            return View(comment);
        }

        //发表文章评论
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult PostComment()
        {
            try
            {
                var postid = Request.QueryString["postid"];
                var nickname = Request.QueryString["nickname"];
                var email = Request.QueryString["email"];
                var siteurl = Request.QueryString["siteurl"];
                var comment = Request.QueryString["comment"];
                var com = new Comment();
                com.CommentID = Guid.NewGuid();
                com.PostID = Guid.Parse(postid);
                com.CommentTime = DateTime.Now;
                com.Content = comment;
                com.NickName = nickname;
                com.Email = email;
                com.SiteUrl = siteurl;
                db.Comments.Add(com);
                db.SaveChanges();
                return Json("评论成功");
            }
            catch { return Json("评论失败"); }
        }

        //根据文章id获取评论
        [HttpPost]
        public ActionResult GetComment(Guid id)
        {
            try
            {
                var comments = db.Comments.Where(n => n.PostID == id).OrderBy(n => n.CommentTime);
                var strHtml = new System.Text.StringBuilder();

                if (comments.Count() == 0)
                    return Json("暂无评论");
                else
                {
                    var count = 1;
                    foreach (var item in comments)
                    {
                        strHtml.Append("<hr/>");
                        strHtml.Append("<div><span>第" + count + "楼 " + item.NickName + " 发表于:" + item.CommentTime + "</span>");
                        strHtml.Append("<br/>-------------------<br/>");
                        strHtml.Append(item.Content);
                        strHtml.Append("</div><br/>");
                        count++;
                    }
                    return Json(strHtml.ToString());
                }
            }
            catch { return Json("获取评论出错"); }
        }

        //删除评论
        [IsAdmin]
        [HttpPost]
        public ActionResult Delete(Guid id)
        {
            try
            {
                Comment comment = db.Comments.Find(id);
                db.Comments.Remove(comment);
                db.SaveChanges();
                return Json("删除成功");
            }
            catch { return Json("删除失败"); }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}